package com.hubu.file;
import com.hubu.BinaryHeap;
import com.hubu.Heap;
import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class FileTopK {
    private Heap<String> heap=null;
    private List<String> result=new ArrayList<>();
    public List<String> topK(String filePath,int k){
        List<String> maxList=new ArrayList<>();
        File file=new File(filePath);
        File[] files = file.listFiles();
        String name=null;
        if(files!=null&&files.length>0){
            for(File item:files){
                if(item.isFile()&&(name=item.getName()).endsWith("txt")){
                    findCurrentFileMaxValue(item,k);
                }
            }
        }
        for(int i=0;i<k;i++){
            if(i<k){
                heap.add(result.get(i));
            }
            else{
                if(result.get(i).compareTo(heap.get())>0){
                    heap.replace(result.get(i));
                }
            }
        }
        List<String> topKList=new ArrayList<>();
        for(int i=0;i<heap.size();i++){
            topKList.add(heap.get(i));
        }
        return topKList;
    }
    public  void findCurrentFileMaxValue(File file,int k){
        long beginTime=System.currentTimeMillis();
        List<String> values=new ArrayList<>();
        if(!file.exists()||file.isDirectory()){
            throw new RuntimeException("文件必须存在并且不能是文件夹");
        }
        FileInputStream fileInputStream=null;
        BufferedReader bufferedReader=null;

        InputStreamReader inputStreamReader=null;

        try {
            fileInputStream=new FileInputStream(file);
            inputStreamReader =new InputStreamReader(fileInputStream);
            bufferedReader=new BufferedReader(inputStreamReader);
            while(true){
                try {
                    String s = bufferedReader.readLine();
                    if(s==null){
                        break;
                    }
                    values.add(s);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            String maxValue=values.get(0);
            if(heap==null){
                heap=new BinaryHeap<>(new Comparator<String>() {
                    //构建小顶堆
                    @Override
                    public int compare(String o1, String o2) {
                        return o2.compareTo(o1);
                    }
                });
            }
            for(int i=0;i<k;i++){
                if(i<k){
                    heap.add(values.get(i));
                }
                if(values.get(i).compareTo(heap.get())>0){
                    heap.replace(values.get(i));
                }
            }
            for(int i=1;i<values.size();i++) {
                if(maxValue.compareTo(values.get(i))<0) {
                    maxValue=values.get(i);
                }
            }
            for(int i=0;i<heap.size();i++){
                result.add(heap.get(i));
            }
            heap.clear();
            //将heap中的数据添加到堆中
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(bufferedReader!=null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(inputStreamReader!=null){
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fileInputStream!=null){
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args) {
        long beginTime=System.currentTimeMillis();
        FileTopK fileTopK = new FileTopK();
        List<String> list = fileTopK.topK("D:\\data2\\data", 1);
        for(String item:list){
            System.out.println(item);
        }
        long endTime=System.currentTimeMillis();
        System.out.println("花费的时间是");
        System.out.println(endTime-beginTime);
    }
}
